package br.unicentro.lynx.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import br.unicentro.lynx.model.factory.ConnectionFactory;
import br.unicentro.lynx.model.pojo.Area;

public class AreaDao {
	private String sql;
	private Connection connection;
	private PreparedStatement statement;
	private ResultSet result;

	public void insert(Area area) {
		open();
		this.sql = "INSERT INTO Areas (description_area) VALUES (?)";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setString(1, area.getDescriptionArea());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void update(Area area) {
		open();
		this.sql = "UPDATE Areas SET description_area = ? WHERE id_area = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setString(1, area.getDescriptionArea());
			this.statement.setInt(2, area.getIdArea());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public void delete(Area area) {
		open();
		this.sql = "DELETE FROM Areas WHERE id_area = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, area.getIdArea());
			this.statement.execute();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public Area search(Integer id_area) {
		open();
		this.sql = "SELECT * FROM Areas WHERE id_area = ?";
		try {
			this.statement = connection.prepareStatement(sql);
			this.statement.setInt(1, id_area);
			this.result = this.statement.executeQuery();
			if (this.result.next()) {
				return new Area(result.getInt("id_area"),
						result.getString("description_area"));
			} else {
				return null;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	public ArrayList<Area> list() {
		open();
		this.sql = "SELECT * FROM Areas";
		ArrayList<Area> areas = new ArrayList<Area>();
		try {
			this.statement = connection.prepareStatement(sql);
			this.result = this.statement.executeQuery();
			while (this.result.next()) {
				areas.add(new Area(result.getInt("id_area"), result
						.getString("description_area")));
			}
			return areas;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				this.result.close();
				close();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}

	private void open() {
		this.connection = ConnectionFactory.getConnection(
				ConnectionFactory.URL, ConnectionFactory.USER,
				ConnectionFactory.PASSWORD);
	}

	private void close() {
		try {
			this.statement.close();
			this.connection.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}